(*
  Copyright (c) 2009 Barry Schwartz

  Permission is hereby granted, free of charge, to any person
  obtaining a copy of this software and associated documentation
  files (the "Software"), to deal in the Software without
  restriction, including without limitation the rights to use,
  copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the
  Software is furnished to do so, subject to the following
  conditions:

  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  OTHER DEALINGS IN THE SOFTWARE.
*)

(*-----------------------------------------------------------------------*)

open Pycaml
open Python_tools
open UniTools
open UTypes
open XNum
  
(*-----------------------------------------------------------------------*)

let log_file = ref None
let log_open file = log_file := Some (open_out file)
  
let log_string str =
  print_string str;
  flush stdout;
  match !log_file with
    | None -> ()
    | Some oc -> output_string oc str
  
let log_uc_list str = log_string (UString.to_string str)
let log_uc_string str = log_string (UString.to_string (Array.to_list str))
let log_uniText u = log_string (UniText.to_string u)
let log_uniChar c = log_string (UniText.to_string (UniText.of_uniChars [| c |]))
let log_int x = log_string (string_of_int x)
let log_num x = log_string (string_of_float (float_of_num x))
  
let log_info (file, line, col) msg =
  if file <> UniText.empty then
    begin
      log_string "\nIn file ";
      log_uniText file;
      log_string ", line ";
      log_int line;
      log_string ", column ";
      log_int col;
      log_string ":\n";
      log_string msg
    end
  else
    begin
      log_string "\n";
      log_string msg
    end
  
let log_warn (file, line, col) msg =
  if file <> UniText.empty then
    begin
      log_string "\nWarning, in file ";
      log_uniText file;
      log_string ", line ";
      log_int line;
      log_string ", column ";
      log_int col;
      log_string ":\n";
      log_string msg
    end
  else
    begin
      log_string "\nWarning: ";
      log_string msg
    end
  
let log_error (file, line, col) msg =
  if file <> UniText.empty then
    begin
      log_string "\nError, in file ";
      log_uniText file;
      log_string ", line ";
      log_int line;
      log_string ", column ";
      log_int col;
      log_string ":\n";
      log_string msg
    end
  else
    begin
      log_string "\nError: ";
      log_string msg
    end

(*-----------------------------------------------------------------------*)

let py_log_open =
  python_interfaced_function
    [| EitherStringType |]
    (fun py_args ->
       log_open (unpythonize_string py_args.(0));
       pynone ())

let _ = register_for_python [| ("log_open", py_log_open) |]

(*-----------------------------------------------------------------------*)
